// kuaidi/pages/rider/index/index.js
const App = getApp();
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isViews:false,
    status: 0,
    use_order:[],//当前点击查看的order
    is_rider: false,
    btnDisabled: false, //禁用抢单按钮
  },
  onViewOrder(e) {
    let _this = this
    let order_id = e.currentTarget.dataset.id
    if(_this.data.status == 0){
      Toast.fail('接单后可查看');
      return false;
    }
    let order = _this.data.order
    let obj = {}
    for(let i in order){
      if(order[i].order_id == order_id){
        obj = order[i]
        break;
      }
    }
    _this.setData({
      use_order:obj
    })
    _this.onIsViews()
  },
  onIsViews(){
    let _this = this
    _this.setData({
      isViews:!_this.data.isViews
    })
  },
  onCreatedOrder(e) {
    let _this = this
    _this.setData({
      btnDisabled: true
    })
    wx.showLoading({
      title: '加载中',
    })
    // 订单提交
    App._post_form('kuaidi.rider.order/add', {
      order_id: e.currentTarget.dataset.id
    }, result => {
      wx.hideLoading();
      _this.setData({
        btnDisabled: false
      })
      _this.getOrder()
    });
  },
  onChangeStatus(e) {
    let _this = this
    this.setData({
      status: e.detail.index
    })
    _this.getOrder()
  },
  onUploadVoucher(e) {
    let _this = this
    _this.setData({
      btnDisabled: true
    })
    // 提交凭证并取件
    let pickUpOrder = function (file_id) {
      wx.showLoading({
        title: '加载中',
      })
      // 订单提交
      App._post_form('kuaidi.rider.order/pick', {
        detail_id: e.currentTarget.dataset.id,
        image_id: file_id
      }, result => {
        wx.hideLoading();
        _this.setData({
          btnDisabled: false
        })
        _this.getOrder()
      });
    }
    // 拍照上传凭证
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        _this.uploadImage(pickUpOrder, tempFilePaths[0])
      },
      complete() {
        _this.setData({
          btnDisabled: false
        })
      }
    })
  },
  uploadImage(callBack, filePath) {
    wx.uploadFile({
      url: App.api_root + 'upload/image',
      filePath: filePath,
      name: 'iFile',
      formData: {
        wxapp_id: App.getWxappId(),
        token: wx.getStorageSync('token')
      },
      success: function (res) {
        let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
        if (result.code === 1) {
          // 执行回调函数
          callBack && callBack(result.data.file_id);
        }
      }
    });
  },
  onChangeTabbar(e) {
    App.riderTabbar(e.detail)
  },
  onConfirmOrder(e) {
    let _this = this
    _this.setData({
      btnDisabled: true
    })
    wx.showLoading({
      title: '加载中',
    })
    App._post_form('kuaidi.rider.order/finish', {
      order_id: e.currentTarget.dataset.id
    }, result => {
      wx.hideLoading();
      _this.setData({
        btnDisabled: false
      })
      _this.getOrder()
    });
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function (result) {
      _this.setData({
        userInfo: result.data.userInfo
      });
    });
  },
  getOrder() {
    let _this = this;
    App._get('kuaidi.rider.order/lists', {
      status: _this.data.status
    }, function (result) {
      _this.setData(result.data)
    });
  },
  getArea() {
    let _this = this
    let area = wx.getStorageSync('area')
    if (area && (area = JSON.parse(area))) {
      _this.setData({
        area: area,
        area_name: area.name
      })
    }
  },
  returnPage() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    _this.getUserDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    _this.getOrder()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})